package bdd.sql.server.configuration;

import ihm.tool.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import tools.PasswordSecurity;

import bdd.sql.server.bean.GlobalServer;
import bdd.sql.server.bean.Camera;
import bdd.sql.server.bean.CameraOptions;
import bdd.sql.server.bean.Encryption;
import bdd.sql.server.bean.Log;
import bdd.sql.server.bean.SiteServer;
import bdd.sql.server.bean.User;
import bdd.sql.server.bean.Video;
import bdd.sql.server.bean.WebServer;
import bdd.sql.server.hibernate.HibernateUtil;

import enumTypes.Civility;
import enumTypes.LogType;
import enumTypes.Rank;

/**
 * Classe de test pour la base de données.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class DatabaseConfiguration {
	
	/**
	 * Données de tests pour la base de données.
	 */
	public static void build() {

		/*
		 * Data related to the architecture:
		 */

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		/*
		 * Adding logs!
		 */
		tx = session.beginTransaction();
		int type = 0;
		for(int i=0; i<50; i++) {
			type = (int) ((Math.random()*i)%3);
			if(type == 0){
				Log obj = new Log(new Date(),
						LogType.Error,
						"machine"+(int) (Math.random()*i),
						"application"+(int) (Math.random()*i),
						"message"+(int) (Math.random()*i));
				session.save(obj);
			}
			else if(type == 1){
				Log obj = new Log(new Date(),
						LogType.Information,
						"machine"+(int) (Math.random()*i),
						"application"+(int) (Math.random()*i),
						"message"+(int) (Math.random()*i));
				session.save(obj);
			}
			else{
				Log obj = new Log(new Date(),
						LogType.Warning,
						"machine"+(int) (Math.random()*i),
						"application"+(int) (Math.random()*i),
						"message"+(int) (Math.random()*i));
				session.save(obj);
			}
		}
		tx.commit();
		
		/*
		 * Adding sites!
		 */
		tx = session.beginTransaction();
		for(int i=0; i<5; i++) {
			SiteServer site = new SiteServer("paris"+i, (int) (Math.random()*i)+"."+(int) (Math.random()*i)+"."+(int) (Math.random()*i)+"."+(int) (Math.random()*i), "cluster_"+i, "key_"+i);
			
			/*
			 * Adding Ciphers!
			 */
			Encryption e = new Encryption("algo"+i+".class");
			session.save(site);
			session.save(e);
			
			site.setEncryption(e);
			session.save(site);
			
			/*
			 * Adding cameras!
			 */
			for(int j=0; j<3; j++) {
				String videoFolder = "C:\\Users\\B182JS\\workspace\\vsm\\video\\";
				//String videoFolder = "C:\\Documents and Settings\\Guillaume\\workspace\\vsm\\video\\";
				Camera cam = new Camera("cam_"+i+"."+j, videoFolder+j+".avi");
				if(i==1 && j==0)
					cam.setCam_ip("dshow://"); // webcam
				
				cam.setSite(site);
				session.save(cam);
				
				/*
				 * Adding videos!
				 */
				for(int k=0; k<10; k++) {
					Video vid = new Video("video_"+i+"."+j+".avi", new Date(), new Date(), new Date(), new Date(), (float)12.5);
					vid.setCamera(cam);
					session.save(vid);
				}
			}
		}
		tx.commit();
		
		/*
		 * Adding CameraOptions!
		 */
		tx = session.beginTransaction();
		CameraOptions opt = new CameraOptions("option_1","type_1", "param_1");
		session.save(opt);
		CameraOptions opt2 = new CameraOptions("option_2","type_2", "param_2");
		session.save(opt2);
		CameraOptions opt3 = new CameraOptions("option_3","type_3", "param_3");
		session.save(opt3);
		CameraOptions opt4 = new CameraOptions("option_4","type_4", "param_4");
		session.save(opt4);
		tx.commit();
		
		/*
		 * Adding Alerts to admin!
		 */
		tx = session.beginTransaction();
		Log alert1 = new Log(new Date(), LogType.Alert,"serveur de site 1", "application", "message 1");
		session.save(alert1);
		Log alert2 = new Log(new Date(), LogType.Alert,"serveur global", "application", "message 2");
		session.save(alert2);
		Log alert3 = new Log(new Date(), LogType.Alert,"applet", "application", "message 3");
		session.save(alert3);
		tx.commit();
		
		
		/*
		 * Adding WebServer & SiteServer & GlobalServer!
		 */
		tx = session.beginTransaction();
		//WebServer ws = new WebServer("webserver_applet", "10.4.101.189", "cluster");
		WebServer ws = new WebServer("webserver_applet", "192.168.2.51", "cluster");
		session.save(ws);
		SiteServer ss = new SiteServer("LO0", "192.168.2.41", "cluster", "");
		Camera cam_test = new Camera("cam_test", "testtest", "osef", "test");
		cam_test.setSite(ss);
		session.save(cam_test);
		session.save(ss);
		GlobalServer gs = new GlobalServer("GL0", "192.168.2.31", "cluster");
		session.save(gs);
		
		Set<SiteServer> sites = gs.getSiteServers();
		sites.add(ss);
		gs.setSiteServers(sites);
		session.save(gs);
		ss.setGlobalServer(gs);
		tx.commit();
		
		//global : 192.168.2.31 / test@test =>
		//local : 192.168.2.41 / test@test => 
		//web : 192.168.2.51 / M1Kraken@Kr4ken
		//changer pour accéder au web :
		//10.4.101.169 (gateway 10.4.0.253 / DNS 10.4.1.1 / masque 255.0.0.0)
		
		//etc/network/interface => /etc/init.d/network restart
		//interface : 192.168.2.31 / 255.255.255.0 / 192.168.2.51
		//=> 10.4.101.65 / 255.255.0.0 / 10.4.0.253
		
		/*
		 * Data related to the applet:
		 */
		
		/*
		 * Add admin
		 */
		// TODO sha256_2
		tx = session.beginTransaction();
		User userAdmin = new User("admin", "a", "admin", Rank.Administrateur, Civility.Monsieur, StringUtils.generateRandomString(12));
		session.save(userAdmin);
		userAdmin.setUser_pwd(PasswordSecurity.sha256("admin",userAdmin.getUser_seedPwd()));
		userAdmin.setUser_firstcon(0);
		session.save(userAdmin);
		tx.commit();
		
		/*
		 * Adding rights!
		 * user_camera table
		 */
		tx = session.beginTransaction();
		@SuppressWarnings("unchecked")
		List<Camera> camList = session.createCriteria(Camera.class).addOrder(Order.asc("cam_id")).list();
		User admin = (User) session.load(User.class, 1);
		
		for(Camera cam : camList) {
			admin.getCameras().add(cam);
			cam.getUsers().add(admin);
			session.save(cam);
		}
		
		tx.commit();
		HibernateUtil.closeSession();
	}
	
	public static void main(String[] args) {
		DatabaseConfiguration.build();
	}

}
